package com.cb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.common.R;
import com.cb.dto.OrdersDto;
import com.cb.entity.Orders;
import com.cb.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据:{}",orders);
        orderService.submit(orders);
        return R.success("下单成功！");
    }

    /**
     * 订单管理
     */
    @GetMapping("/userPage")
    public R<IPage<OrdersDto>> userPage(int page,int pageSize){
        //构造分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        IPage<OrdersDto> ordersDtoIPage = orderService.userPage(ordersPage);
        return R.success(ordersDtoIPage);
    }

    /**
     * 再来一单
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        orderService.again(orders);
        return R.success("再来一单！");
    }

    /**
     * 管理端查询订单
     */
    @GetMapping("/page")
    public R<IPage<OrdersDto>> page(int page, int pageSize, String number,String beginTime,String endTime){
        System.out.println("page = " + page + ", pageSize = " + pageSize + ", number = " + number + ", beginTime = " + beginTime + ", endTime = " + endTime);
        //构造分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        IPage<OrdersDto> ordersDtoIPage=orderService.pageOrders(ordersPage,number,beginTime,endTime);
        return R.success(ordersDtoIPage);
    }

    /**
     * 订单派送
     */
    @PutMapping
    public R<String> send(@RequestBody Orders orders){
        orderService.send(orders);
        return R.success("派送成功！");
    }

}
